// 封装接口方法，url统一管理
// 每个方法负责请求一个url地址
// 导入这个方法，就可以发请求
import request from '@/utils/request.js'

// 登录接口
export const loginAPI = ({
  mobile,
  code
}) => request({
  url: '/v1_0/authorizations',
  method: 'POST',
  data: {
    mobile,
    code
  }
})

// 用户关注
export const userFollowedAPI = ({
  userId
}) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: userId
  }
})

// 取消关注
export const userUnFollowedAPI = ({
  userId
}) => request({
  url: `/v1_0/user/followings/${userId}`,
  method: 'DELETE'
})

// 频道获取
export const getAllChannelsAPI = () => request({
  url: '/v1_0/channels',
  method: 'GET'
})

// 获取用户选择的频道,未登录获取默认频道
export const getUserChannelsAPI = () => request({
  url: '/v1_0/user/channels',
  method: 'GET'
})

// 更新覆盖频道
export const updateChannelsAPI = ({
  channels
}) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels
  }
})

// 删除用户指定频道
export const removeTheChannelsAPI = ({
  channelId
}) => request({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'DELETE'
})

// 文章获取
export const getAllArticleListAPI = ({
  channel_id,
  timestamp
}) => request({
  url: '/v1_0/articles',
  method: 'GET',
  params: {
    channel_id,
    timestamp
  }
})

// 文章 反馈 不感兴趣
export const dislikeArticleAPI = ({
  artId
}) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  data: {
    target: artId
  }
})

// 文章 反馈 垃圾内容
export const reportArticleAPI = ({
  artId,
  type,
  remark
}) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  data: {
    target: artId,
    type: type,
    remark: '无内容'
  }
})

// 文章 获取详情
export const detailAPI = ({
  artId
}) => request({
  url: `/v1_0/articles/${artId}`
})

// 评论列表
export const commentsListAPI = ({
  id,
  offset = null,
  limit = 10
}) => request({
  url: '/v1_0/comments',
  params: {
    type: 'a',
    source: id,
    offset,
    limit
  }
})

// 评论 - 喜欢
export const commentLikingAPI = ({
  comId
}) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}

// 评论-取消喜欢
export const commentDisLikingAPI = ({
  comId
}) => {
  return request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}

// 点赞
export const likeArticleAPI = ({
  artId
}) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})

// 取消点赞
export const unLikeArticleAPI = ({
  artId
}) => request({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})

// 文章 发布评论
export const commentSendAPI = ({
  id,
  content,
  art_id = null
}) => {
  const obj = {
    target: id,
    content
  }
  if (art_id !== null) {
    obj.art_id = art_id
  }
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}

// 搜索 联想菜单
export const suggestListAPI = ({
  keywords
}) => request({
  url: '/v1_0/suggestion',
  params: {
    q: keywords
  }
})

// 搜索 结果列表
export const searchResultAPI = ({
  page = 1,
  per_page = 20000,
  q
}) => request({
  url: '/v1_0/search',
  params: {
    page,
    per_page,
    q
  }
})

// 个人
export const userProfileAPI = () => request({
  url: '/v1_0/user/profile'
})

// 用户获取基本信息
export const getUserInfoAPI = () => request({
  url: '/v1_0/user'
})

// 用户更新头像
export const updateUserPhotoAPI = (fd) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  data: fd
})

// 用户 更新资料
export const updateUserProfileAPI = (dataObj) => {
  const obj = {
    name: '',
    gender: 0,
    birthday: '',
    intro: ''
  }

  for (const prop in obj) {
    if (dataObj[prop] === undefined) {
      delete obj[prop]
    } else {
      obj[prop] = dataObj[prop]
    }
  }

  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: obj
  })
}

// 用户 刷新token
export const getNewTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: 'Bearer' + localStorage.getItem('refresh_token')
  }
})
